
package com.sausageking.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.Embedded;
import javax.persistence.Id;
import javax.validation.ConstraintViolation;

import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Unindexed;
import com.googlecode.objectify.condition.IfNotNull;

@Unindexed
@Cached
public class Merchant implements Serializable {

  private static final long serialVersionUID = 1741913071619572101L;

  @Id
  @Indexed
  private Long id;

  @Indexed(IfNotNull.class)
  private String name;

  private String description;

  @Indexed
  private String email;

  @Indexed(IfNotNull.class)
  private String phone;

  @Indexed(IfNotNull.class)
  private String website;

  // address
  private String street;

  private String city;

  private String state;

  private String zip;

  private String photoUrl;

  @Embedded
  private Offer offer;

  private Date creationTime;

  private String timeZoneId = "America/New_York";// http://joda-time.sourceforge.net/timezones.html
  
  private String salt;

  public Merchant() {
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public boolean hasId() {
    return id != null && id > 0;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getWebsite() {
    return website;
  }

  public void setWebsite(String website) {
    this.website = website;
  }

  public String getStreet() {
    return street;
  }

  public void setStreet(String street) {
    this.street = street;
  }

  public String getState() {
    return state;
  }

  public void setState(String state) {
    this.state = state;
  }

  public String getZip() {
    return zip;
  }

  public void setZip(String zip) {
    this.zip = zip;
  }

  public static long getSerialversionuid() {
    return serialVersionUID;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getCity() {
    return city;
  }

  public Collection<ConstraintViolation<Merchant>> validate() {
    return new ArrayList<ConstraintViolation<Merchant>>();
  }

  public String toString() {
    @SuppressWarnings("deprecation")
    ToStringHelper helper = Objects
        .toStringHelper(this)
        .add("id", id)
        .add("name", name)
        .add("description", description)
        .add("phone", phone)
        .add("website", website)
        .add("street", street)
        .add("city", city)
        .add("state", state)
        .add("zip", zip)
        .add("email", email)
        .add("created",
            creationTime == null ? "NA" : creationTime.toGMTString())
        .add("timezone", timeZoneId)
        .add("salt", salt);
    if (offer != null) {
      helper.add("loyalty offer", offer.toString());
    }
    return helper.toString();
  }

  public void setOffer(Offer offer) {
    this.offer = offer;
  }

  public Offer getOffer() {
    return offer;
  }

  public String getPhotoUrl() {
    return photoUrl;
  }

  public void setPhotoUrl(String photoUrl) {
    this.photoUrl = photoUrl;
  }

  public Date getCreationTime() {
    return creationTime;
  }

  public void setCreationTime(Date creationTime) {
    this.creationTime = creationTime;
  }

  public String getTimeZoneId() {
    return (timeZoneId == null || timeZoneId.length() == 0)
        ? "America/New_York"
        : timeZoneId;
  }

  public void setTimeZoneId(String timeZoneId) {
    this.timeZoneId = timeZoneId;
  }

  public String getSalt() {
    return salt;
  }

  public void setSalt(String salt) {
    this.salt = salt;
  }
}
